<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
require_once '../model/db.php';
?>

<h1>Ho Y Phi</h1>
<?php
$query = "SELECT * FROM companies";
$companies = getData($query);
?>
<?php
$query = "SELECT * FROM apps";
$apps = getData($query);
?>
<form method="POST">
    Min fans<input type="text" name="min_fans"><br>
    Max fans<input type="text" name="max_fans"><br>
    Keyword <input type="text" name="keyword"><br>
    Company<select name="id_company"><br>
        <?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
        <?php endforeach; ?>
        </select><br>
    
        App<select name="id_app">
         
<?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
        <?php endforeach; ?>
    </select>
    <input type="submit" name="submit">
</form>

<?php if (isset($_POST['submit'])): ?>
    <?php
    $min_fans = $_POST['min_fans']; //TODO
    $max_fans = $_POST['max_fans']; //TODO
    $keyword = $_POST['keyword']; //TODO
    $query = '';
    if (empty($keyword)) {
        if (empty($min_fans) && empty($max_fans)) {
            $query = "SELECT * FROM pages";
            $pages = getData($query);
        } elseif (empty($max_fans)) {
            $query = "SELECT * FROM pages 
                     WHERE (likes >= $min_fans)
                     LIMIT 0, 10
                 ";
            $pages = getData($query);
        } elseif (empty($min_fans)) {
            $query = "SELECT * FROM pages 
                     WHERE (likes <= $max_fans)
                     LIMIT 0, 10";
            $pages = getData($query);
        } else {
            $query = "SELECT * FROM pages 
                WHERE (likes <= $max_fans)
                     AND (likes >= $min_fans)
                 LIMIT 0, 10
                 ";
            $pages = getData($query);
        }
    } else {
        if (empty($min_fans) && empty($max_fans)) {
            $query = "SELECT * FROM pages
                         where(description LIKE '% $keyword %') ";
            $pages = getData($query);
        } elseif (empty($max_fans)) {
            $query = "SELECT * FROM pages 
                     WHERE (likes >= $min_fans)
                         AND (description LIKE '% $keyword %')
                     LIMIT 0, 10
                 ";
            $pages = getData($query);
        } elseif (empty($min_fans)) {
            $query = "SELECT * FROM pages 
                     WHERE (likes <= $max_fans)
                         AND (description LIKE '% $keyword %')
                     LIMIT 0, 10
                 ";
            $pages = getData($query);
        } else {
            $query = "SELECT * FROM pages 
                     WHERE (likes <= $max_fans)
                         AND (likes >= $min_fans)
                         AND (description LIKE '% $keyword %')
                     LIMIT 0, 10";
            $pages = getData($query);
        }
    }
    var_dump($query);
    //   $query = "SELECT * FROM pages 
    //             WHERE (likes <= $max_fans)
    //                 AND (likes >= $min_fans)
    //                 AND (description LIKE '% $keyword %')
    //             LIMIT 0, 10
    //             ";
    //   $pages = getData($query);
    ?>

    <?php if (!empty($pages)): ?>
        <table border="2">
            <tr>
                <td> profile </td>
                <td> likes </td>
                <td> description </td>
            </tr>
        <?php foreach ($pages as $page): ?>
                <tr>
                    <td><img src="<?php echo $page['picture'] ?>"></td>
                    <td><?php echo $page['likes'] ?></td>
                    <td><?php echo substr($page['description'], 0, 100) ?></td>
                <?php // var_dump($page);die();?>
                </tr>
        <?php endforeach; ?>
        </table> 
        lam viec nghiem tuc.
        otherwise : get ZEORO
        thay coi zay sao em lam
        cu viec lam, ko biet minh se chi
    <?php endif; ?>
<?php endif; ?>
